<?php
require ("../global.inc.php");
class SysLoginManage extends SystemLoginFrameWork{
	/**
	 * 权限对象
	 *
	 * @var obj
	 */
	var $obj_system_power;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;

	function main(){
		/**
		 * 创建权限对象
		 */
		if (!is_object($this->obj_system_power)){
			require_once("system_power.class.php");
			$this->obj_system_power = new SystemPowerClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}
		
		switch ($this->_input['action']){
			case "check_login":
				$this->_check_login();
				break;
			default:
				$this->_login();
				break;
		}

	}
	
	/**
	 * 判断管理员是否登录
	 */
	function _login(){
		//判断是否已经登录
		if($_SESSION['s_admin'] != ''){
			@header("Location: index.php?action=main");
			exit;
		}
		$this->showpage('sys_index.login');
	}

	/**
	 * 验证管理员登陆
	 */
	function _check_login(){
		/**
		 * 验证登录信息
		 */
		$this->obj_validate->validateparam = array(
		array("input"=>$this->_input["sys_login_name"], "require"=>"true", "message"=>'请输入管理员帐号'),
		array("input"=>$this->_input["sys_password"], "require"=>"true", "message"=>'请输入密码'),
		array("input"=>strtoupper($this->_input['code']),"require"=>"true","validator"=>"Compare","operator"=>"==","to"=>strtoupper($_SESSION['seccode']),"message"=>'验证码不正确')
		);
		$error = $this->obj_validate->validate();
		if($error != ""){
			$this->redirectPath("error","./",$error,1);
		}
		/**
		 * 取管理员信息
		 */
		$member_array = $this->obj_system_power->checkSysMemberExit($this->_input);
		if (empty($member_array)){
			$this->redirectPath("error","./",'用户名或密码错误',1);
		}
		/**
		 * 更新登录时间和次数
		 */
		$result = $this->obj_system_power->updateLoginSysMember($member_array['sys_member_id']);
		if ($result !== true) {
			$this->redirectPath("error","./",'操作失败',1);
		}
		/**
		 * 取管理员权限
		 */
		$power_group = $this->obj_system_power->getSysGroupById($member_array['sys_group_id']);
		/**
		 * 写入session 中
		 */
		$_SESSION['s_admin']['id'] = $member_array['sys_member_id'];
		$_SESSION['s_admin']['name'] = $member_array['sys_login_name'];
		$_SESSION['s_admin']['group_name'] = $power_group['sys_group_name'];
		$_SESSION['s_admin']['power'] = $power_group['sys_group_power'];
		$_SESSION['s_admin']['password'] = $member_array['sys_password'];
		//记录操作日志
		SystemPowerClass::addSysLog('管理员'.$member_array['sys_login_name'].'登录');
		$this->redirectPath("error","./index.php?action=main",'登录成功，正在跳转...',1);
	}
}
$objLogin = new SysLoginManage();
$objLogin->main();
unset($objLogin);
?>